unit uFrmTable;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Math, StdCtrls, Oracle, uutils, uimagepanel, usqls, jpeg, StrUtils,
  pngimage, ComCtrls, oracledata, Buttons, uMainUtils, ufrmcashgameplayerout, DB, gnugettext, ufrmorderdrink, ufrmsetrake,
  Grids, ufrmaddonrebuy, uFrmSubjectImage, ufrmdealerselect, ScktComp;

type
  TFrmTable = class(TForm)
    pgc_Table: TPageControl;
    tbs_Table: TTabSheet;
    img_Table: TImage;
    tbs_Player: TTabSheet;
    img_Player: TImage;
    img_Seat: TImage;
    pnl_TopPlayer: TPanel;
    Edit1: TEdit;
    Button1: TButton;
    img_SeatPop: TImage;
    img_SeatSmall: TImage;
    img_SeatPopSmall: TImage;
    OracleQueryTable: TOracleQuery;
    pnl_PlayerMenu: TPanel;
    btn_PlayerFromTable: TSpeedButton;
    lbl_Tournament: TLabel;
    lbl_Table: TLabel;
    btn_SetDealer: TSpeedButton;
    lbl_TableStatus: TLabel;
    ods_BreakTable: TOracleDataSet;
    ods_FreeTablePositions: TOracleDataSet;
    img_SeatArriving: TImage;
    btn_AcceptPlayer: TSpeedButton;
    lbl_PlayerID: TLabel;
    btn_RebuyAddon: TSpeedButton;
    stg_PlayerData: TStringGrid;
    img_Country: TImage;
    img_SeatDealer: TImage;
    lbl_Message: TLabel;
    tmr_ScrollMsg: TTimer;
    ClientSocket1: TClientSocket;
    mem_PlayerNotes: TMemo;
    Label1: TLabel;
    btn_SetRake: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btn_BackClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure img_TableClick(Sender: TObject);
    procedure btn_PlayerFromTableClick(Sender: TObject);
    procedure CreateParams(var Params: TCreateParams); override;
    procedure btn_SetDealerClick(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure btn_AcceptPlayerClick(Sender: TObject);
    procedure lbl_TableStatusClick(Sender: TObject);
    procedure btn_RebuyAddonClick(Sender: TObject);
    procedure stg_PlayerDataDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
    procedure tmr_ScrollMsgTimer(Sender: TObject);
    procedure ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
    procedure FormDestroy(Sender: TObject);
    procedure btn_SetRakeClick(Sender: TObject);
  private
    pnl_Seat, pnl_Dealer: TImagePanel;
    SizeKoef: double;
    PlayerImage: TImage;
    m_TableName, m_CashGameID: string;
    MustSetDealer, IsRebuy: boolean;
    FullRgn, ClientRgn, CtlRgn: THandle;
    MainUtils: TMainUtils;
    FrmSubjectImage: TFrmSubjectImage;
    procedure PositionSeats(Seats: Integer);
    function PopulateSeats(TableId: string; IsCashGame: boolean; DataSet: TOracleDataSet): boolean;
    procedure BreakTableToTables(TournamentID: string);
    procedure GetTableSeatsPopulation(TournamentID: string);
    { Private declarations }
  public
    { Public declarations }
    StateOrderDrink, StateInfoPlayer: boolean;
    SeatPositioning: boolean;
    m_IsCashGame: boolean;
    m_TournamentID, m_TableID, AddValue: string;
    SmallTableWidth: Integer;
    procedure CheckMessages();
    procedure MakeTransparent;
    procedure OnPlayerClick(Sender: TObject);
    procedure OnDealerClick(Sender: TObject);
    function GetData(TableId: string; IsCashGame: boolean; DataSet, DataSetTables: TOracleDataSet): boolean; // returns true if players on table
    procedure GetPlayerDetails(PlayerID: string);
    procedure RefreshTableView();
    procedure WriteTableStatus(DataSetTable: TOracleDataSet);
    procedure SendEventToServer(EventType, TournamentID, TableId: string);
  end;

var
  FrmTable: TFrmTable;

const
  FormOrigWidth: Integer = 508;

implementation

{$R *.dfm}

procedure TFrmTable.RefreshTableView;
begin
  LockWindowUpdate(self.Handle);
  GetHallDataSetsData();
  if m_IsCashGame then
  begin
    GetData(m_TableID, m_IsCashGame, OracleDataSetCashGame, OracleDataTables);
  end
  else
  begin
    GetData(m_TableID, m_IsCashGame, OracleDataSetTournament, OracleDataTables);
  end;
  pnl_PlayerMenu.Visible := false;
  LockWindowUpdate(0);
end;

procedure TFrmTable.stg_PlayerDataDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  s: string;
  drawrect: TRect;
begin
  s := stg_PlayerData.Cells[ACol, ARow];
  stg_PlayerData.Canvas.Brush.Color := stg_PlayerData.Color;
  stg_PlayerData.Canvas.Font.Color := clwhite;
  drawrect := Rect;
  stg_PlayerData.Canvas.fillrect(drawrect);
  DrawText(stg_PlayerData.Canvas.Handle, Pchar(s), Length(s), Rect, dt_wordbreak or DT_LEFT);
end;

procedure TFrmTable.tmr_ScrollMsgTimer(Sender: TObject);
begin
  tmr_ScrollMsg.Enabled := false;
  LockWindowUpdate(self.Handle);
  lbl_Message.Visible := not lbl_Message.Visible;
  LockWindowUpdate(0);
  tmr_ScrollMsg.Enabled := true;
end;

procedure TFrmTable.CheckMessages();
begin
  // check messages
  OraQueryMainUtils.Session := OraSessMainUtils;
  OraQueryMainUtils.Close;
  OraQueryMainUtils.SQL.Clear;
  OraQueryMainUtils.SQL.Text := ('select name from INSPECTOR_CALLS ic join tables ta on ic.table_id=ta.id where verified=0');
  OraQueryMainUtils.Execute;
  lbl_Message.Caption := '';
  tmr_ScrollMsg.Enabled := false;
  while not OraQueryMainUtils.Eof do
  begin
    lbl_Message.Caption := lbl_Message.Caption + OraQueryMainUtils.Field(0) + ', ';
    OraQueryMainUtils.Next;
    lbl_Message.BringToFront;
    tmr_ScrollMsg.Enabled := true;
  end;
  if lbl_Message.Caption <> '' then
  begin
    lbl_Message.Caption := _('Call Inspector from') + ' ' + lbl_Message.Caption;
  end;
  /// //
end;

procedure TFrmTable.ClientSocket1Error(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
  ErrorCode := 0; // this line solve your problems about asyncroneus socket error
end;

procedure TFrmTable.ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
var
  EventType, TournamentID, TableId: string;
begin
  uutils.SocketTextParse(Socket.ReceiveText, EventType, TournamentID, TableId);
  if (EventType = EventTypeTicket) or (EventType = EventTypeFromTable) and (pgc_Table.ActivePage = tbs_Table) then
  begin
    RefreshTableView();
  end
  else if (EventType = EventTypeCallIns) and (pgc_Table.ActivePage = tbs_Table) then
  begin
    CheckMessages();
  end
  else if (EventType = EventTypeBreakTable) and (pgc_Table.ActivePage = tbs_Table) then
  begin
    RefreshTableView();
  end;
end;

procedure TFrmTable.GetTableSeatsPopulation(TournamentID: string);
var
  x_TableID, Is_Dealer: string;
  x_Seats, x_Seat_Num, i, PosValue: Integer;
  OraQueryIns: TOracleQuery;
begin
  OraQueryIns := TOracleQuery.Create(self);
  OraQueryIns.Session := OraSessMainUtils;
  OraQueryMainUtils.Clear;
  OraQueryMainUtils.SQL.Text := ('DELETE FROM TEMP_TABLE_FREE_SEATS');
  OraQueryMainUtils.Execute;
  OraQueryMainUtils.Clear;
  OraQueryMainUtils.SQL.Text := ('DELETE FROM TEMP_TABLE_POP_SEATS');
  OraQueryMainUtils.Execute;
  OraQueryMainUtils.Clear;
  OraQueryMainUtils.SQL.Text := ('DELETE FROM TEMP_TABLE_SEATS');
  OraQueryMainUtils.Execute;
  // GET POPULATED SEATS
  OraQueryMainUtils.Clear;
  OraQueryMainUtils.SQL.Text := (
    'select (tt.table_id), nvl(seat_num,0) as seat_num, nvl(ta.seats,0) as seats, nvl(IS_DEALER,0) as is_dealer,deleted, nvl(case when dealer_seat<seat_num then seats+(dealer_seat-seat_num) else (dealer_seat-seat_num) end,0)'
      + ' as pos_value from tournament_tables tt       left join tables ta on ta.id=tt.table_id left join (select seat_num as dealer_seat, table_id from TICKETS where is_dealer=1 and deleted=0)' +
      '  tip on tip.table_id=TT.table_id   left join tickets ti on ti.TOURNAMENT_ID=tt.TOURNAMENT_ID and ti.table_id=tt.table_id and ti.deleted=0 where ta.disabled=0 and deleted=0 and tt.tournament_id='
      + TournamentID + '  and ti.out_datetime is null order by table_id, seat_num');
  OraQueryMainUtils.Execute;
  while not OraQueryMainUtils.Eof do
  begin
    x_TableID := OraQueryMainUtils.Field('table_id');
    x_Seats := StrToInt(OraQueryMainUtils.Field('seats'));
    x_Seat_Num := StrToInt(OraQueryMainUtils.Field('seat_num'));
    Is_Dealer := (OraQueryMainUtils.Field('is_dealer'));
    PosValue := (OraQueryMainUtils.Field('pos_value')) + 1;
    OraQueryIns.Clear;
    OraQueryIns.SQL.Text := 'INSERT INTO TEMP_TABLE_POP_SEATS (TABLE_ID,SEATS,SEAT_NUM,IS_DEALER, POS_VALUE) VALUES (' + x_TableID + ',' + IntToStr(x_Seats) + ',' + IntToStr(x_Seat_Num)
      + ',' + Is_Dealer + ',' + IntToStr(PosValue) + ')';
    OraQueryIns.Execute;
    OraQueryMainUtils.Next;
  end;
  OraSessMainUtils.Commit;
  // GET ALL SEATS
  OraQueryMainUtils.Clear;
  OraQueryMainUtils.SQL.Text := ('select tt.table_id, seats, nvl(ttp.seat_num,0) as dealer_seat from tournament_tables tt join tables ta on ta.id=tt.table_id' +
      ' left join (select seat_num, table_id from TEMP_TABLE_POP_SEATS where is_dealer=1) ttp on ttp.table_id=tt.table_id' + ' where ta.disabled=0 and tt.tournament_id=' + TournamentID);
  OraQueryMainUtils.Execute;
  while not OraQueryMainUtils.Eof do
  begin
    x_TableID := OraQueryMainUtils.Field('table_id');
    x_Seats := StrToInt(OraQueryMainUtils.Field('seats'));
    for i := 1 to x_Seats do
    begin
      OraQueryIns.Clear;
      OraQueryIns.SQL.Text := 'INSERT INTO TEMP_TABLE_SEATS (TABLE_ID,SEATS,SEAT_NUM) VALUES (' + x_TableID + ',' + IntToStr(x_Seats) + ',' + IntToStr(i) + ')';
      OraQueryIns.Execute;
    end;
    OraQueryMainUtils.Next;
  end;
  // GET FREE SEATS
  OraQueryMainUtils.Clear;
  OraQueryMainUtils.SQL.Text := ('select table_id, nvl(seats,0) as seats,nvl(seat_num,0) as seat_NUM,nvl(seat_pos,0) as seat_pos from GET_FREE_SEAT_POS_VALUE');
  OraQueryMainUtils.Execute;
  while not OraQueryMainUtils.Eof do
  begin
    x_TableID := OraQueryMainUtils.Field('table_id');
    x_Seats := StrToInt(OraQueryMainUtils.Field('seats'));
    x_Seat_Num := StrToInt(OraQueryMainUtils.Field('seat_num'));
    PosValue := StrToInt(OraQueryMainUtils.Field('seat_pos'));
    if PosValue <> 0 then
    begin
      OraQueryIns.Clear;
      OraQueryIns.SQL.Text := 'INSERT INTO TEMP_TABLE_FREE_SEATS (TABLE_ID,SEATS,SEAT_NUM,POS_VALUE) VALUES (' + x_TableID + ',' + IntToStr(x_Seats) + ',' + IntToStr(x_Seat_Num) + ',' + IntToStr
        (PosValue) + ')';
      OraQueryIns.Execute;
    end;
    OraQueryMainUtils.Next;
  end;
  OraSessMainUtils.Commit;
end;

procedure TFrmTable.btn_AcceptPlayerClick(Sender: TObject);
begin
  OraQueryMainUtils.Close;
  OraQueryMainUtils.SQL.Clear;
  OraQueryMainUtils.SQL.Text := ('UPDATE TICKETS SET TABLE_ID=BREAK_TO_TABLE_ID, SEAT_NUM=BREAK_TO_SEAT_NUM WHERE ID=' + pnl_PlayerMenu.Hint);
  OraQueryMainUtils.Execute;
  OraQueryMainUtils.Close;
  OraQueryMainUtils.SQL.Clear;
  OraQueryMainUtils.SQL.Text := ('UPDATE TICKETS SET BREAK_TO_TABLE_ID=NULL, BREAK_TO_SEAT_NUM=NULL WHERE ID=' + pnl_PlayerMenu.Hint);
  OraQueryMainUtils.Execute;
  OraSessMainUtils.Commit;
  // check if is last break position
  OraQueryMainUtils.Close;
  OraQueryMainUtils.SQL.Clear;
  OraQueryMainUtils.SQL.Text := ('SELECT * FROM TICKETS WHERE BREAK_TO_TABLE_ID IS NOT NULL');
  OraQueryMainUtils.Execute;
  if OraQueryMainUtils.Eof then
  begin
    // breaked table get from tournament tables
    OraQueryMainUtils.Close;
    OraQueryMainUtils.SQL.Clear;
    OraQueryMainUtils.SQL.Text := ('SELECT TT.ID FROM TABLES TA LEFT JOIN TOURNAMENT_TABLES TT ON TT.TABLE_ID=TA.ID AND TT.TOURNAMENT_ID=' + m_TournamentID + ' WHERE IS_BREAK_TABLE=1');
    OraQueryMainUtils.Execute;
    if not OraQueryMainUtils.Eof then
    begin
      DeleteDataFromDB('TOURNAMENT_TABLES', 'ID=' + VarToStr(OraQueryMainUtils.Field(0)));
    end;
    // clear breaking table data
    uMainUtils.ClearBreakTableData();
  end;
  SendEventToServer(EventTypeTicket, m_TournamentID, m_TableID);
  RefreshTableView();
end;

procedure TFrmTable.SendEventToServer(EventType, TournamentID, TableId: string);
begin
  try
    if not ClientSocket1.Active then
    begin
      ClientSocket1.Open;
      ClientSocket1.Active := true;
    end;
  finally
    ClientSocket1.Socket.SendText(EventType + '|1|' + TournamentID + '|2|' + TableId);
  end;
end;

procedure TFrmTable.btn_BackClick(Sender: TObject);
begin
  pgc_Table.ActivePage := tbs_Table;
end;

procedure TFrmTable.btn_PlayerFromTableClick(Sender: TObject);
begin
  if MessageDlg(_('Player from table?'), mtWarning, [mbYes, mbNo], 0) = mrYes then
  begin
    if m_IsCashGame then
    begin
      FrmCashGamePlayerOut.ShowForm(pnl_PlayerMenu.Hint);
    end
    else
    begin
      MainUtils.TournamentPlayerFromTable(pnl_PlayerMenu.Hint, m_TournamentID, '', '');
      SendEventToServer(EventTypeFromTable, m_TournamentID, m_TableID);
    end;
    RefreshTableView();
  end;
end;

procedure TFrmTable.btn_RebuyAddonClick(Sender: TObject);
var
  GameID, RebuyOrAddon: string;
  Confirmed: boolean;
begin
  AddValue := '';
  Confirmed := false;
  if not m_IsCashGame then
  begin
    // check if its already addon time
    OraQueryMainUtils.Close;
    OraQueryMainUtils.SQL.Clear;
    OraQueryMainUtils.SQL.Text := ('SELECT REBUY_OR_ADDON FROM GETPRESENTTOURNAMENTROUNDDATA WHERE TOURNAMENT_ID=' + m_TournamentID);
    OraQueryMainUtils.Execute;
    RebuyOrAddon := VarToStr(OraQueryMainUtils.Field('REBUY_OR_ADDON'));
    if (RebuyOrAddon = 'ADDON') and IsRebuy then
    begin
      IsRebuy := false;
      btn_RebuyAddon.Caption := 'ADDON';
    end;
    // ----
    Confirmed := (MessageDlg(_('Confirm') + ' ' + btn_RebuyAddon.Caption + ' ' + 'for player?', mtWarning, [mbYes, mbNo], 0) = mrYes);
  end;
  if m_IsCashGame then
  begin
    FrmRebuyAddon.ShowModal;
    AddValue := FrmRebuyAddon.AddValue;
    GameID := m_CashGameID
  end
  else
  begin
    GameID := m_TournamentID;
  end;
  if Confirmed or m_IsCashGame then
  begin
    InsertRebuyOrAddon(m_IsCashGame, IsRebuy, true, pnl_PlayerMenu.Hint, AddValue, GameID);
    SendEventToServer(EventTypeTicket, m_TournamentID, m_TableID);
  end;
  pnl_PlayerMenu.Visible := false;
end;

procedure TFrmTable.btn_SetDealerClick(Sender: TObject);
begin
  if not m_IsCashGame then
  begin
    // SET IS DEALER
    UpdateDataToDB('TICKETS', 'IS_DEALER=1', 'ID=' + pnl_PlayerMenu.Hint);
    // check if is the last player for select dealer
    OraQueryMainUtils.Close;
    OraQueryMainUtils.SQL.Clear;
    OraQueryMainUtils.SQL.Text := ('SELECT * FROM TOURNAMENT_TABLES TT JOIN TABLES TA ON TA.ID=TT.TABLE_ID WHERE TA.ID<>' + m_TableID + ' AND TT.TOURNAMENT_ID=' + m_TournamentID +
        ' AND TA.MUST_SET_DEALER=1');
    OraQueryMainUtils.Execute;
    if OraQueryMainUtils.Eof then
    BEGIN
      GetTableSeatsPopulation(m_TournamentID);
      // BREAK TABLE ACTION!
      BreakTableToTables(m_TournamentID);
      // CLEAR IS DELAER ON BREAKED TABLE
      UpdateDataToDB('TICKETS', 'IS_DEALER=0', 'TABLE_ID NOT IN (SELECT ID FROM TABLES WHERE IS_BREAK_TABLE=1)  AND TOURNAMENT_ID=' + m_TournamentID);
    END;
    // CLEAR MUST SET DEALER FLAG ON TABLE
    UpdateDataToDB('TABLES', 'MUST_SET_DEALER=0', 'ID=' + m_TableID);
  end;
  SendEventToServer(EventTypeTicket, m_TournamentID, m_TableID);
  RefreshTableView();
end;

procedure TFrmTable.btn_SetRakeClick(Sender: TObject);
begin
  FrmSetRake.ShowSetRakeForm(m_CashGameID, m_TableID);
end;

procedure TFrmTable.BreakTableToTables(TournamentID: string);
var
  BreakPosVal, FreePosValue, FreeTableId, TicketID, BreakTableId: string;
  QueryTicketID: TOracleQuery;
begin
  // get break table positions
  ods_FreeTablePositions.Session := OraSessMainUtils;
  ods_BreakTable.Session := OraSessMainUtils;
  ods_BreakTable.Close;
  ods_BreakTable.SQL.Clear;
  ods_BreakTable.SQL.Text := ('SELECT * FROM TEMP_TABLE_POP_SEATS TTP JOIN TABLES TA ON TA.ID = TTP.TABLE_ID WHERE IS_BREAK_TABLE=1 ORDER BY TABLE_ID, POS_VALUE');
  ods_BreakTable.Open;
  // get free tables and positions
  ods_FreeTablePositions.Session := OraSessMainUtils;
  ods_FreeTablePositions.Close;
  ods_FreeTablePositions.SQL.Clear;
  ods_FreeTablePositions.SQL.Text := ('SELECT * FROM TEMP_TABLE_FREE_SEATS TTP LEFT JOIN TABLES TA ON TA.ID = TTP.TABLE_ID WHERE IS_BREAK_TABLE IS NULL OR  IS_BREAK_TABLE<>1 ORDER BY POS_VALUE');
  ods_FreeTablePositions.Open;
  QueryTicketID := TOracleQuery.Create(self);
  QueryTicketID.Session := OraSessMainUtils;
  while not ods_BreakTable.Eof do
  begin
    if ods_FreeTablePositions.Eof then
    begin
      ShowMessage(_('Free positions are to small'));
      exit;
    end;
    BreakPosVal := ods_BreakTable.FieldValues['SEAT_NUM'];
    FreePosValue := ods_FreeTablePositions.FieldValues['SEAT_NUM'];
    FreeTableId := ods_FreeTablePositions.FieldValues['TABLE_ID'];
    BreakTableId := ods_BreakTable.FieldValues['TABLE_ID'];
    QueryTicketID.Close;
    QueryTicketID.SQL.Clear;
    QueryTicketID.SQL.Text := ('select ID from tickets where tournament_id=' + TournamentID + ' and table_id=' + BreakTableId + ' and seat_num=' + BreakPosVal +
        ' and out_datetime is null and deleted=0');
    QueryTicketID.Execute;
    TicketID := QueryTicketID.Field(0);
    UpdateDataToDB('TICKETS', 'BREAK_TO_SEAT_NUM=' + FreePosValue + ',BREAK_TO_TABLE_ID=' + FreeTableId, 'ID=' + TicketID);
    ods_FreeTablePositions.Next;
    ods_BreakTable.Next;
  end;
  // CLEAR IS DEALER FLAG ON OTHER TABLES
  UpdateDataToDB('TICKETS', 'IS_DEALER=0', 'TOURNAMENT_ID=' + m_TournamentID);
  QueryTicketID.Close;
  freeandnil(QueryTicketID);
end;

procedure TFrmTable.Button1Click(Sender: TObject);
begin
  PositionSeats(StrToInt(Edit1.Text));
end;

procedure TFrmTable.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TFrmTable.FormCreate(Sender: TObject);
begin
  StateOrderDrink := false;
  StateInfoPlayer := false;
  SeatPositioning := false;
  Brush.Style := bsClear;
  BorderStyle := bsNone;
  pgc_Table.ActivePage := tbs_Table;
  PlayerImage := TImage.Create(self);
  SmallTableWidth := 300;
  FrmSubjectImage := TFrmSubjectImage.Create(self);
  FrmRebuyAddon := TFrmRebuyAddon.Create(self);
  lbl_Message.Caption := '';
  lbl_TableStatus.Caption := '';
  MainUtils := TMainUtils.Create();
  gnugettext.TranslateComponent(self);
end;

procedure TFrmTable.FormDestroy(Sender: TObject);
begin
  ClientSocket1.Close;
  ClientSocket1.Active := false;
end;

procedure TFrmTable.FormResize(Sender: TObject);
var
  BorderThick: Integer;
begin
  BorderThick := 6;
  pgc_Table.Width := self.Width + (2 * BorderThick);
  pgc_Table.Height := self.Height + (2 * BorderThick);
  pgc_Table.Left := -BorderThick;
  pgc_Table.Top := -BorderThick;
end;

procedure TFrmTable.MakeTransparent;
var
  AControl: TControl;
  A, Margin, X, Y, CtlX, CtlY: Integer;
begin
  Margin := (Width - ClientWidth) div 2;
  FullRgn := CreateRectRgn(0, 0, Width, Height);
  X := Margin;
  Y := Height - ClientHeight - Margin;
  ClientRgn := CreateRectRgn(X, Y, X + ClientWidth, Y + ClientHeight);
  CombineRgn(FullRgn, FullRgn, ClientRgn, RGN_DIFF);
  for A := 0 to ControlCount - 1 do
  begin
    AControl := Controls[A];
    if (AControl is TWinControl) or (AControl is TGraphicControl) then
      with AControl do
      begin
        if Visible then
        begin
          CtlX := X + Left;
          CtlY := Y + Top;
          CtlRgn := CreateRectRgn(CtlX, CtlY, CtlX + Width, CtlY + Height);
          CombineRgn(FullRgn, FullRgn, CtlRgn, RGN_OR);
        end;
      end;
  end;
  SetWindowRgn(Handle, FullRgn, true);
end;

procedure TFrmTable.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  Params.ExStyle := Params.ExStyle OR WS_EX_TRANSPARENT;
end;

procedure TFrmTable.WriteTableStatus(DataSetTable: TOracleDataSet);
begin
  lbl_TableStatus.Visible := false;
  if (DataSetTable.FieldValues['MUST_SET_DEALER'] <> null) or (DataSetTable.FieldValues['IS_BREAK_TABLE'] <> null) then
  begin
    if StrToB(DataSetTable.FieldValues['MUST_SET_DEALER']) or StrToB(DataSetTable.FieldValues['IS_BREAK_TABLE']) then
    begin
      lbl_TableStatus.Visible := true;
      if StrToB(DataSetTable.FieldValues['MUST_SET_DEALER']) then
      begin
        lbl_TableStatus.Caption := _('MARK DELAER IN NEXT GAME');
      end;
      if uutils.StrToB(DataSetTable.FieldValues['IS_BREAK_TABLE']) then
      begin
        lbl_TableStatus.Caption := _('TABLE FOR BREAKING');
      end;
      if StrToB(DataSetTable.FieldValues['IS_BREAK_TABLE']) and StrToB(DataSetTable.FieldValues['MUST_SET_DEALER']) then
      begin
        lbl_TableStatus.Caption := _('MARK DEALER IN LAST GAME!' + #13#10 + 'TABLE FOR BREAKING');
      end;
      lbl_TableStatus.BringToFront;
    end
  end;
end;

function TFrmTable.GetData(TableId: string; IsCashGame: boolean; DataSet, DataSetTables: TOracleDataSet): boolean;
var
  RebuyOrAddon: string;
begin
  Canvas.Lock;
  m_IsCashGame := IsCashGame;
  m_TableID := TableId;
  m_TournamentID := '';
  MustSetDealer := false;
  DataSetTables.Session := OraSessMainUtils;
  DataSetTables.Filtered := true;
  DataSetTables.Filter := 'ID=' + TableId;
  DataSet.Filtered := true;
  DataSet.Filter := 'TABLE_ID=' + TableId;
  if not DataSetTables.Eof then
  begin
    m_TableName := VarToStr(DataSetTables.FieldValues['NAME']);
    PositionSeats(DataSetTables.FieldValues['SEATS']);
    lbl_Table.Font.Color := $00400080;
    if not m_IsCashGame then
    begin
      { if VarToStr(DataSet.FieldValues['STATUS']) = 'RUNNING' then
        begin
        lbl_Table.Font.Color := ColorTournamentRunning;
        end
        else if VarToStr(DataSet.FieldValues['STATUS']) = 'PAUSED' then
        begin
        lbl_Table.Font.Color := ColorTournamentPaused;
        end
        else if VarToStr(DataSet.FieldValues['STATUS']) = 'SEATING' THEN
        begin
        lbl_Table.Font.Color := ColorTournamentSeating;
        end
        else if VarToStr(DataSet.FieldValues['STATUS']) = 'FINISHED' THEN
        begin
        lbl_Table.Font.Color := ColorTournamentFinished;
        end
        else if VarToStr(DataSet.FieldValues['STATUS']) = 'ANNOUNCED' THEN
        begin
        lbl_Table.Font.Color := ColorTournamentAnnounced;
        end; }
    end;
    lbl_Table.Caption := #13#10 + m_TableName;
    lbl_Tournament.Caption := '';
  end;
  result := PopulateSeats(TableId, IsCashGame, DataSet);
  if Width > SmallTableWidth then
  begin
    // ---
    lbl_Tournament.Visible := false;
    // if m_TournamentID <> '' then
    lbl_Table.Visible := false;
  end
  else
  begin
    lbl_Tournament.Visible := true;
    lbl_Table.Visible := true;
  end;
  OracleQueryTable.Session := OraSessMainUtils;
  if IsCashGame then
  begin
    btn_RebuyAddon.Caption := 'REBUY';
    btn_RebuyAddon.Visible := true;
    if VarToStr(DataSet.FieldValues['CASH_GAME_ID']) <> '' then
    begin
      OracleQueryTable.SQL.Clear;
      OracleQueryTable.SQL.Text := 'SELECT NAME, RAKE_ID FROM CASH_GAMES WHERE ID=' + VarToStr(DataSet.FieldValues['CASH_GAME_ID']);
      OracleQueryTable.Execute;
      lbl_Tournament.Caption := VarToStr(OracleQueryTable.Field(0));
      btn_SetRake.Visible := false;
      if Width > SmallTableWidth then
      begin
        btn_SetRake.Visible := (OracleQueryTable.Field(1) <> null);
        btn_SetRake.Left := (self.Width div 2) - (btn_SetRake.Width div 2);
        btn_SetRake.Top := (self.Height div 2) - (btn_SetRake.Height div 2);
        btn_SetRake.BringToFront;
      end;
    end
    else
    begin
      btn_SetRake.Visible := false;
    end;
  end
  else
  begin
    btn_SetRake.Visible := false;
    if m_TournamentID <> '' then
    begin
      WriteTableStatus(DataSetTables);
      OracleQueryTable.SQL.Clear;
      OracleQueryTable.SQL.Text := 'SELECT NAME FROM TOURNAMENTS WHERE ID=' + m_TournamentID;
      OracleQueryTable.Execute;
      if Width > SmallTableWidth then
      begin
        DataSet.Filtered := true;
        DataSet.Filter := 'TOURNAMENT_ID=' + m_TournamentID;
        RebuyOrAddon := VarToStr(DataSet.FieldValues['REBUY_OR_ADDON']);
        IsRebuy := false;
        btn_RebuyAddon.Visible := false;
        if (RebuyOrAddon = 'REBUY') or (RebuyOrAddon = 'ADDON') then
        begin
          if (RebuyOrAddon = 'REBUY') then
          begin
            IsRebuy := true;
          end;
          btn_RebuyAddon.Caption := RebuyOrAddon;
          btn_RebuyAddon.Visible := true;
        end;
        DataSet.Filtered := false;
      end;
      lbl_Tournament.Caption := VarToStr(OracleQueryTable.Field(0));
    end;
  end;
  Canvas.unLock;
end;

procedure TFrmTable.GetPlayerDetails(PlayerID: string);
VAR
  CountryID: string;
begin
  pgc_Table.ActivePage := self.tbs_Player;
  img_Player.Picture := uutils.GetImage(SQL_Player_Picture + PlayerID, OraSessMainUtils, true);
  OraQueryMainUtils.Close;
  OraQueryMainUtils.SQL.Clear;
  OraQueryMainUtils.SQL.Text := ('select pl.name, country_id, surname,birth_date, co.name as country from players pl left join countries co on pl.COUNTRY_ID=co.id where pl.id=' + PlayerID);
  OraQueryMainUtils.Execute;
  if not OraQueryMainUtils.Eof then
  begin
    CountryID := OraQueryMainUtils.Field('COUNTRY_ID');
    if CountryID <> '' then
    begin
      img_Country.Picture := uutils.GetImage('SELECT IMAGE, IMAGE_TYPE FROM COUNTRIES WHERE ID=' + CountryID, OraSessMainUtils);
    end;
    stg_PlayerData.Cells[1, 0] := OraQueryMainUtils.Field('NAME') + ' ' + OraQueryMainUtils.Field('SURNAME');
    stg_PlayerData.Cells[1, 1] := OraQueryMainUtils.Field('BIRTH_DATE');
    stg_PlayerData.Cells[1, 2] := OraQueryMainUtils.Field('COUNTRY');
  end
  else
  begin
    stg_PlayerData.Cells[1, 0] := '';
    stg_PlayerData.Cells[1, 1] := '';
    stg_PlayerData.Cells[1, 2] := '';
  end;
  stg_PlayerData.Cells[0, 0] := '';
  stg_PlayerData.Cells[0, 0] := '';
  stg_PlayerData.Cells[0, 0] := '';
  // ---
  OraQueryMainUtils.Close;
  OraQueryMainUtils.SQL.Clear;
  OraQueryMainUtils.SQL.Text := ('select * from player_notes where player_id=' + PlayerID);
  OraQueryMainUtils.Execute;
  mem_PlayerNotes.Lines.Clear;
  while not OraQueryMainUtils.Eof do
  begin
    mem_PlayerNotes.Lines.Add(OraQueryMainUtils.Field(0));
    OraQueryMainUtils.Next;
  end;

end;

procedure TFrmTable.img_TableClick(Sender: TObject);
begin
  pnl_PlayerMenu.Visible := false;
end;

procedure TFrmTable.lbl_TableStatusClick(Sender: TObject);
begin
  if Width > SmallTableWidth then
  begin
    pnl_PlayerMenu.Visible := false;
  end;
end;

procedure TFrmTable.OnDealerClick(Sender: TObject);
begin
  if Width > SmallTableWidth then
  begin
    // check if dealer is looged in
    OraQueryMainUtils.SQL.Clear;
    OraQueryMainUtils.SQL.Text := 'SELECT * FROM TABLE_SUBJECT_LOGINS WHERE IN_DATETIME IS NOT NULL AND OUT_DATETIME IS NULL AND TABLE_ID=' + m_TableID;
    OraQueryMainUtils.Execute;
    if OraQueryMainUtils.Eof then
    begin
      // login dealer
      FrmDealerSelect.ShowDealerForm(m_TableID, m_TournamentID, m_CashGameID, '', '');
      RefreshTableView();
    end
    else
    begin
      // logout dealer
      if MessageDlg(_('Delaer out?'), mtWarning, [mbYes, mbNo], 0) = mrYes then
      begin
        UpdateDataToDB('TABLE_SUBJECT_LOGINS', 'OUT_DATETIME=(SELECT SYSDATE FROM DUAL)', 'TABLE_ID=' + m_TableID + ' AND OUT_DATETIME IS NULL');
        RefreshTableView();
      end;
    end;
  end;
end;

procedure TFrmTable.OnPlayerClick(Sender: TObject);
var
  TicketID: string;
  PnlPlayerHeight: Integer;
  IsAcceptPos, IsBreakTable, MustSetDealer: boolean;
begin
  if Width > SmallTableWidth then
  begin
    TicketID := TImage(Sender).Hint;
    if TicketID = '' then // sender is player status label
    begin
      TicketID := TImagePanel(TLabel(Sender).Parent).Image.Hint;
    end;
    if (TicketID <> '') then
    begin
      lbl_PlayerID.Caption := TLabel(TImage(Sender).Parent).Hint;
      if lbl_PlayerID.Caption = '' then // sender is player status label
      begin
        lbl_PlayerID.Caption := TImagePanel(TLabel(Sender).Parent).Hint;
      end;
      if StateOrderDrink then
      begin
        FrmOrderDrink.ShowOrderDrinkForm(lbl_PlayerID.Caption, m_TableID, TicketID);
      end
      else if StateInfoPlayer then
      begin
        GetPlayerDetails(lbl_PlayerID.Caption);
      end
      else
      begin
        LockWindowUpdate(self.Handle);
        pnl_PlayerMenu.Left := TImagePanel(TImage(Sender).Parent).Left;
        pnl_PlayerMenu.Top := TImagePanel(TImage(Sender).Parent).Top + TImagePanel(TImage(Sender).Parent).Height - 40;
        pnl_PlayerMenu.Width := TImagePanel(TImage(Sender).Parent).Width;
        pnl_PlayerMenu.Hint := TicketID;
        pnl_PlayerMenu.BringToFront;
        btn_SetDealer.Visible := false;
        btn_AcceptPlayer.Visible := false;
        // -----is player accept
        if not m_IsCashGame then
        begin
          OraQueryMainUtils.SQL.Clear;
          OraQueryMainUtils.SQL.Text := 'SELECT * FROM TICKETS TI JOIN TABLES TA ON TI.TABLE_ID=TA.ID WHERE TI.ID=' + TicketID;
          OraQueryMainUtils.Execute;
          if not OraQueryMainUtils.Eof then
          begin
            IsAcceptPos := VarToStr(OraQueryMainUtils.Field('BREAK_TO_SEAT_NUM')) <> '';
            IsBreakTable := StrToB(OraQueryMainUtils.Field('IS_BREAK_TABLE'));
            MustSetDealer := StrToB(OraQueryMainUtils.Field('MUST_SET_DEALER'));
          end;
          if IsAcceptPos then
          begin
            if m_TableID = OraQueryMainUtils.Field('BREAK_TO_TABLE_ID') then
            begin
              btn_AcceptPlayer.Visible := true;
              btn_SetDealer.Visible := false;
              btn_PlayerFromTable.Visible := false;
              btn_RebuyAddon.Visible := false;
            end
            else
            begin
              // player away waiting to accept on other table
              btn_SetDealer.Visible := false;
              btn_PlayerFromTable.Visible := false;
              btn_AcceptPlayer.Visible := false;
              btn_RebuyAddon.Visible := false;
            end;
          end
          else
          begin
            if MustSetDealer then
            begin
              btn_SetDealer.Visible := true;
              btn_AcceptPlayer.Visible := false;
              btn_PlayerFromTable.Visible := false;
              btn_RebuyAddon.Visible := false;
            end
            else
            begin
              btn_SetDealer.Visible := false;
              btn_AcceptPlayer.Visible := false;
              btn_PlayerFromTable.Visible := true;
            end;
          end;
        end;
        // set height of panel menu
        PnlPlayerHeight := 0;

        begin
          pnl_PlayerMenu.Visible := true;
          if btn_RebuyAddon.Visible then
          begin
            PnlPlayerHeight := PnlPlayerHeight + btn_RebuyAddon.Height;
          end;
          if btn_SetDealer.Visible then
          begin
            PnlPlayerHeight := PnlPlayerHeight + btn_SetDealer.Height;
          end;
          if btn_AcceptPlayer.Visible then
          begin
            PnlPlayerHeight := PnlPlayerHeight + btn_AcceptPlayer.Height;
          end;
          if btn_PlayerFromTable.Visible then
          begin
            PnlPlayerHeight := PnlPlayerHeight + btn_PlayerFromTable.Height;
          end;
          pnl_PlayerMenu.Height := PnlPlayerHeight;
        end;
        LockWindowUpdate(0);
      end;
    end;
  end;
end;

function TFrmTable.PopulateSeats(TableId: string; IsCashGame: boolean; DataSet: TOracleDataSet): boolean;
var
  i, X, Y: Integer;
  BreakTablePersonAway, IsDealer: boolean;
  SubjectID: string;
  ABitmap: TBitmap;
begin
  Canvas.Lock;
  result := false;
  // get dealer
  OracleDataDealers.Open;
  OracleDataDealers.Filtered := true;
  OracleDataDealers.Filter := 'TABLE_ID=' + TableId;
  SubjectID := VarToStr(OracleDataDealers.FieldValues['USER_ID']);
  if SubjectID <> '' then
  begin
    FrmSubjectImage.img_TakenPhoto.Picture.Assign(uutils.GetImage(SQL_User_Picture + SubjectID, OraSessMainUtils, false));
    FrmSubjectImage.img_BlackHole.BringToFront;
    pnl_Dealer.Image.Transparent := true;
    pnl_Dealer.Image.Picture.Bitmap.Assign(FrmSubjectImage.GetFormImage);
    pnl_Dealer.Image.Picture.Bitmap.PixelFormat := pf24Bit;
    pnl_Dealer.PlayerName.Caption := VarToStr(OracleDataDealers.FieldValues['SUBJECT']);
    pnl_Dealer.PlayerName.BringToFront;
  end
  else
  begin
    pnl_Dealer.Image.Picture := img_SeatDealer.Picture;
  end;
  // ------
  DataSet.Session := OraSessMainUtils;
  DataSet.Filtered := true;
  // get players on tables
  DataSet.Open;
  DataSet.Filter := 'TABLE_ID=' + TableId;
  DataSet.First;
  if not DataSet.Eof then
  begin
    if IsCashGame then
    begin
      m_CashGameID := VarToStr(DataSet.FieldValues['CASH_GAME_ID']);
    end
    else
    begin
      m_TournamentID := VarToStr(DataSet.FieldValues['TOURNAMENT_ID']);
    end;
    while not DataSet.Eof do
    begin
      if VarToStr(DataSet.FieldValues['PLAYER_ID']) <> '' then
      begin
        BreakTablePersonAway := false;
        IsDealer := false;
        if not IsCashGame then
        begin
          if (DataSet.FieldValues['BREAK_TO_TABLE_ID']) <> null then
          begin
            BreakTablePersonAway := true;
          end;
          if VarToStr(DataSet.FieldValues['IS_DEALER']) = '1' then
          begin
            IsDealer := true;
          end;
        end;
        for i := 0 to ComponentCount - 1 do
        begin
          if Components[i] is TImagePanel then
          begin
            if midstr(TImagePanel(Components[i]).Name, 5, Length(TImagePanel(Components[i]).Name)) = VarToStr(DataSet.FieldValues['SEAT_NUM']) then
            begin
              result := true;
              if self.Width > SmallTableWidth then
              begin
                if BreakTablePersonAway then
                begin
                  TImagePanel(Components[i]).PlayerStatus.Caption := 'AWAY' + #13#10 + VarToStr(DataSet.FieldValues['BREAK_TABLE_NAME']) + '-' + VarToStr(DataSet.FieldValues['BREAK_TO_SEAT_NUM']);
                  TImagePanel(Components[i]).PlayerStatus.Font.Color := clRed;
                end
                else if IsDealer then
                begin
                  TImagePanel(Components[i]).PlayerStatus.Caption := 'DEALER' + #13#10 + VarToStr(DataSet.FieldValues['BREAK_TABLE_NAME']) + '-' + VarToStr(DataSet.FieldValues['BREAK_TO_SEAT_NUM']);
                  TImagePanel(Components[i]).PlayerStatus.Font.Color := CLGreen;
                end;
                TImagePanel(Components[i]).CountryImage.Picture := uutils.GetImage('SELECT IMAGE, IMAGE_TYPE FROM (SELECT COUNTRY_ID FROM PLAYERS WHERE ID=' + VarToStr
                    (DataSet.FieldValues['PLAYER_ID']) + ') A1 JOIN COUNTRIES C1 ON A1.COUNTRY_ID=C1.ID', OraSessMainUtils);
                TImagePanel(Components[i]).PlayerName.Caption := VarToStr(DataSet.FieldValues['NAME']);
                TImagePanel(Components[i]).OnClick := OnPlayerClick;

                // -------
                FrmSubjectImage.img_TakenPhoto.Picture.Assign(uutils.GetImage(SQL_Player_Picture + VarToStr(DataSet.FieldValues['PLAYER_ID']), OraSessMainUtils, true));
                FrmSubjectImage.img_BlackHole.BringToFront;
                // -------
                if DataSet.FieldValues['TICKET_ID'] <> null then
                begin
                  TImagePanel(Components[i]).Image.Transparent := true;
                  TImagePanel(Components[i]).Image.Picture.Bitmap.Assign(FrmSubjectImage.GetFormImage);
                  TImagePanel(Components[i]).Image.Picture.Bitmap.PixelFormat := pf24Bit;
                  TImagePanel(Components[i]).OnClick := OnPlayerClick;
                  TImagePanel(Components[i]).Image.Hint := VarToStr(DataSet.FieldValues['TICKET_ID']);
                  TImagePanel(Components[i]).Hint := VarToStr(DataSet.FieldValues['PLAYER_ID']);
                end;
              end
              else
              begin
                TImagePanel(Components[i]).Image.Picture := img_SeatPopSmall.Picture;
              end;
            end;
          end;
        end;
      end;
      DataSet.Next;
    end;
    // get breaked players on new tables prepare for accept
    if not IsCashGame then
    begin
      DataSet.Filter := 'BREAK_TO_TABLE_ID=' + TableId;
      DataSet.First;
      if not DataSet.Eof then
      begin
        m_TournamentID := VarToStr(DataSet.FieldValues['TOURNAMENT_ID']);
        while not DataSet.Eof do
        begin
          if VarToStr(DataSet.FieldValues['PLAYER_ID']) <> '' then
          begin
            for i := 0 to ComponentCount - 1 do
            begin
              if Components[i] is TImagePanel then
              begin
                if midstr(TImagePanel(Components[i]).Name, 5, Length(TImagePanel(Components[i]).Name)) = VarToStr(DataSet.FieldValues['BREAK_TO_SEAT_NUM']) then
                begin
                  result := true;
                  if self.Width > SmallTableWidth then
                  begin
                    TImagePanel(Components[i]).PlayerName.Caption := VarToStr(DataSet.FieldValues['NAME']);
                    TImagePanel(Components[i]).PlayerStatus.Caption := 'ACCEPT' + #13#10 + VarToStr(DataSet.FieldValues['BREAK_TABLE_NAME']) + '-' + VarToStr(DataSet.FieldValues['BREAK_TO_SEAT_NUM']);
                    TImagePanel(Components[i]).PlayerStatus.Font.Color := clRed;
                    PlayerImage.Picture.Assign(uutils.GetImage(SQL_Player_Picture + VarToStr(DataSet.FieldValues['PLAYER_ID']), OraSessMainUtils, true));
                    if PlayerImage.Picture <> nil then
                    begin
                      TImagePanel(Components[i]).Image.Picture := PlayerImage.Picture;
                      TImagePanel(Components[i]).OnClick := OnPlayerClick;
                      TImagePanel(Components[i]).Image.Hint := VarToStr(DataSet.FieldValues['TICKET_ID']);
                      TImagePanel(Components[i]).Hint := VarToStr(DataSet.FieldValues['PLAYER_ID']);
                    end;
                  end
                  else
                  begin
                    TImagePanel(Components[i]).Image.Picture := img_SeatArriving.Picture;
                  end;
                  TImagePanel(Components[i]).Refresh;
                  break;
                end;
              end;
            end;
          end;
          DataSet.Next;
        end;
      end;
    end;
  end;
  Canvas.unLock;
end;

procedure TFrmTable.PositionSeats(Seats: Integer);
var
  i, pnlClientWidth, pnlClientHeight, MaxSeatsClientHeight: Integer;
begin
  Canvas.Lock;
  SeatPositioning := true;
  ClientWidth := Parent.Width;
  ClientWidth := ClientWidth;
  ClientHeight := trunc((ClientWidth) / 2);
  SizeKoef := ClientWidth / FormOrigWidth;
  Font.Size := trunc(12 * SizeKoef);
  lbl_Table.Font.Size := trunc(30 * SizeKoef);
  lbl_Tournament.Font.Size := trunc(24 * SizeKoef);
  lbl_TableStatus.BringToFront;
  pnlClientWidth := trunc(65 * SizeKoef);
  pnlClientHeight := trunc(55 * SizeKoef);
  MaxSeatsClientHeight := self.ClientHeight;
  for i := ComponentCount - 1 downto 0 do
  begin
    if Components[i] is TImagePanel then
    begin
      Components[i].Free;
    end;
  end;
  case Seats of
    1:
      begin
        // first
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.Left := 0;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Name := 'SEAT1';
      end;
    2:
      begin
        // first
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := 0;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight;
        pnl_Seat.Name := 'SEAT1';
        // last
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight;
        pnl_Seat.Name := 'SEAT2';
      end;
    3:
      begin
        // first
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := 0;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight;
        pnl_Seat.Name := 'SEAT1';
        // center
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width div 2 - pnlClientWidth div 2;
        pnl_Seat.Top := tbs_Table.Height - MaxSeatsClientHeight;
        pnl_Seat.Name := 'SEAT2';
        // last
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight;
        pnl_Seat.Name := 'SEAT3';
      end;
    4:
      begin
        // first
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 16);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 12);
        pnl_Seat.Name := 'SEAT1';
        // ----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := (tbs_Table.Width div 3 - pnlClientWidth div 2) - (tbs_Table.Width div 3 - pnlClientWidth div 2) div 2;
        pnl_Seat.Top := (tbs_Table.Height - (MaxSeatsClientHeight div 2)) - (tbs_Table.Height - (MaxSeatsClientHeight div 2)) div 2;
        pnl_Seat.Name := 'SEAT2';
        // ----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := (tbs_Table.Width - tbs_Table.Width div 3 - pnlClientWidth div 2) + (tbs_Table.Width div 3 - pnlClientWidth div 2) div 2;
        pnl_Seat.Top := (tbs_Table.Height - (MaxSeatsClientHeight div 2)) - (tbs_Table.Height - (MaxSeatsClientHeight div 2)) div 2;
        pnl_Seat.Name := 'SEAT3';
        // last
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight;
        pnl_Seat.Name := 'SEAT4';
      end;
    5:
      begin
        // first
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 16);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 12);
        pnl_Seat.Name := 'SEAT1';
        // ----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := (tbs_Table.Width div 3 - pnlClientWidth div 2) - (tbs_Table.Width div 3 - pnlClientWidth div 2) div 2;
        pnl_Seat.Top := (tbs_Table.Height - (MaxSeatsClientHeight div 2)) - (tbs_Table.Height - (MaxSeatsClientHeight div 2)) div 2;
        pnl_Seat.Name := 'SEAT2';
        // center
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width div 2 - pnlClientWidth div 2;
        pnl_Seat.Top := tbs_Table.Height - MaxSeatsClientHeight;
        pnl_Seat.Name := 'SEAT3';
        // ----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := (tbs_Table.Width - tbs_Table.Width div 3 - pnlClientWidth div 2) + (tbs_Table.Width div 3 - pnlClientWidth div 2) div 2;
        pnl_Seat.Top := (tbs_Table.Height - (MaxSeatsClientHeight div 2)) - (tbs_Table.Height - (MaxSeatsClientHeight div 2)) div 2;
        pnl_Seat.Name := 'SEAT4';
        // last
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 16);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 12);
        pnl_Seat.Name := 'SEAT5';
      end;
    6:
      begin
        // first
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 75);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 12);
        pnl_Seat.Name := 'SEAT1';
        // ----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 85);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 70);
        pnl_Seat.Name := 'SEAT2';
        // ----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 60);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 97);
        pnl_Seat.Name := 'SEAT3';
        // center
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 33);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 97);
        pnl_Seat.Name := 'SEAT4';
        // ----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 8);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 70);
        pnl_Seat.Name := 'SEAT5';
        // last
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 16);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 12);
        pnl_Seat.Name := 'SEAT6';
      end;
    7:
      begin
        // first
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 26) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 12);
        pnl_Seat.Name := 'SEAT1';
        // ----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 12) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 50);
        pnl_Seat.Name := 'SEAT2';
        // ----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 28) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 97);
        pnl_Seat.Name := 'SEAT3';
        // center
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 55) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 100);
        pnl_Seat.Name := 'SEAT4';
        // ----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 79) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 97);
        pnl_Seat.Name := 'SEAT5';
        // ----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 98) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 50);
        pnl_Seat.Name := 'SEAT6';
        // last
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 80) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 12); ;
        pnl_Seat.Name := 'SEAT7';
      end;
    8:
      begin
        // first
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 26) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 12);
        pnl_Seat.Name := 'SEAT1';
        // -----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 12) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 40);
        pnl_Seat.Name := 'SEAT2';
        // -----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 20) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 85);
        pnl_Seat.Name := 'SEAT3';
        // -----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 40) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 98);
        pnl_Seat.Name := 'SEAT4';
        // center
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width div 2 - pnlClientWidth div 2 + trunc(tbs_Table.Width / 100 * 5);
        pnl_Seat.Top := tbs_Table.Height - MaxSeatsClientHeight;
        pnl_Seat.Name := 'SEAT5';
        // ----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 20);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 93);
        pnl_Seat.Name := 'SEAT6';
        // ----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 5);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 50);
        pnl_Seat.Name := 'SEAT7';
        // last
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 22);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 12);
        pnl_Seat.Name := 'SEAT8';
      end;
    9:
      begin
        // first
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 26) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 12);
        pnl_Seat.Name := 'SEAT1';
        // -----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 13) - pnlClientWidth; ;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 40);
        pnl_Seat.Name := 'SEAT2';
        // -----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 21) - pnlClientWidth; ;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 82);
        pnl_Seat.Name := 'SEAT3';
        // -----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 37) - pnlClientWidth; ;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 93); ;
        pnl_Seat.Name := 'SEAT4';
        // center
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 45);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 95);
        pnl_Seat.Name := 'SEAT5';
        // -----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 27);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 93);
        pnl_Seat.Name := 'SEAT6';
        // -----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 11);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 82);
        pnl_Seat.Name := 'SEAT7';
        // -----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 5);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 40);
        pnl_Seat.Name := 'SEAT8';
        // last
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 16);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 12);
        pnl_Seat.Name := 'SEAT9';
      end;
    10:
      begin
        // first
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 28) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 12);
        pnl_Seat.Name := 'SEAT1';
        // -----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Color := clRed;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 14) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 35);
        pnl_Seat.Name := 'SEAT2';
        // -----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Color := clRed;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 15) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 70);
        pnl_Seat.Name := 'SEAT3';
        // -----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Color := clRed;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 30) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 92);
        pnl_Seat.Name := 'SEAT4';
        // center left
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Color := clRed;
        pnl_Seat.Left := trunc(tbs_Table.Width / 100 * 47) - pnlClientWidth;
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 98);
        pnl_Seat.Name := 'SEAT5';
        // -----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Color := clRed;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 36);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 98);
        pnl_Seat.Name := 'SEAT6';
        // -----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Color := clRed;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 18);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 92);
        pnl_Seat.Name := 'SEAT7';
        // -----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Color := clRed;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 5);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 70);
        pnl_Seat.Name := 'SEAT8';
        // -----
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Color := clRed;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 4);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 35);
        pnl_Seat.Name := 'SEAT9';
        // last
        pnl_Seat := TImagePanel.Create(self);
        pnl_Seat.Parent := tbs_Table;
        pnl_Seat.ClientWidth := pnlClientWidth;
        pnl_Seat.ClientHeight := pnlClientHeight;
        pnl_Seat.Left := tbs_Table.Width - pnlClientWidth - trunc(tbs_Table.Width / 100 * 18);
        pnl_Seat.Top := tbs_Table.Height - pnlClientHeight - trunc((tbs_Table.Height - pnlClientHeight) / 100 * 12);
        pnl_Seat.Name := 'SEAT10';
      end;
  end;
  // -----
  // set font sizes
  for i := ComponentCount - 1 downto 0 do
  begin
    if Components[i] is TImagePanel then
    begin
      TImagePanel(Components[i]).Image.OnClick := OnPlayerClick;
      TImagePanel(Components[i]).PlayerStatus.OnClick := OnPlayerClick;
      TImagePanel(Components[i]).PlayerName.OnClick := OnPlayerClick;
      TImagePanel(Components[i]).Caption := '';
      TImagePanel(Components[i]).PlayerName.Font.Size := trunc(8 * SizeKoef);
      TImagePanel(Components[i]).PlayerStatus.Font.Size := trunc(10 * SizeKoef);
      if self.Width > SmallTableWidth then
      begin
        TImagePanel(Components[i]).Image.Picture := img_Seat.Picture;
      end
      else
      begin
        TImagePanel(Components[i]).Image.Picture := img_SeatSmall.Picture;
      end;
    end;
  end;
  // create delaer seat
  pnl_Dealer := TImagePanel.Create(self);
  pnl_Dealer.Parent := tbs_Table;
  pnl_Dealer.Image.OnClick := OnDealerClick;
  pnl_Dealer.Width := trunc(pnlClientWidth);
  pnl_Dealer.Height := trunc(pnlClientHeight);
  pnl_Dealer.PlayerStatus.OnClick := OnDealerClick;
  pnl_Dealer.PlayerName.OnClick := OnDealerClick;
  pnl_Dealer.Image.Picture := img_SeatDealer.Picture;
  pnl_Dealer.PlayerName.Font.Size := trunc(8 * SizeKoef);
  pnl_Dealer.PlayerStatus.Font.Size := trunc(10 * SizeKoef);
  pnl_Dealer.Left := tbs_Table.Width div 2 - pnl_Dealer.Width div 2;
  pnl_Dealer.Top := tbs_Table.Height - pnl_Dealer.Height - trunc((15 * SizeKoef));
  pnl_Dealer.Visible := true;
  SeatPositioning := false;
  Canvas.unLock;
end;

end.
